﻿using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Ymt.Repository;
using YmtRpc.Domain;
using YmtRpc.Domain.Enum;

namespace YmtRpc.Domain
{

    /// <summary>
    /// RPC客户端配置的仓储类
    /// </summary>
    public class RpcClientconfigRepository : MySQLRepository<RpcClientconfig>, IRpcClientconfigRepository
    {
        public RpcClientconfigRepository()
            : base()
        {

        }

        /// <summary>
        /// 获取RPC客户端配置列表
        /// </summary>
        /// <returns></returns>
        public List<RpcClientconfig> GetList(string appId)
        {
            List<RpcClientconfig> list = new List<RpcClientconfig>();
            string sql = "select * from rpcclientconfig";
            ParametersWrapper par = new ParametersWrapper();

            if (!string.IsNullOrEmpty(appId))
            {
                sql += " WHERE AppId = @AppId";

                par.Add("AppId", appId);
            }

            var result = Query(sql, par.Build());
            if (result != null && result.Any())
            {
                list = result.ToList();
            }

            return list;
        }

        /// <summary>
        /// 获取应用程序列表
        /// </summary>
        /// <returns></returns>
        public List<string> GetAppList()
        {
            List<RpcClientconfig> list = new List<RpcClientconfig>();

            string sql = "select AppId from rpcclientconfig";
            ParametersWrapper par = new ParametersWrapper();

            var result = Query(sql, par.Build());
            if (result != null && result.Any())
            {
                list = result.ToList();
            }

            return list.Select(r=>r.AppId).ToList();
        }
    }
}
